<?php
error_reporting(E_ALL &~E_NOTICE);

define('NO_REGISTER_GLOBALS', 1);
define('THIS_SCRIPT', 'gcbos');
define('USING_AJAX', array_key_exists('ajax', $_REQUEST));

$phrasegroups = array();
$specialtemplates = array();
$actiontemplates = array();
$globaltemplates = array();

require_once('./global.php');
require_once(DIR . '/includes/class_gcbos.php');

$start = intval($_REQUEST['start']);
$limit = intval($_REQUEST['limit']);
$last = $start;

if ((!USING_AJAX) && ($start < 0)) $start = 0;
if (($limit <= 0) || ($limit > $vbulletin->options['gcbos_message_limit'])) $limit = $vbulletin->options['gcbos_message_limit'];
if ($last < 0) $last = 0;

$result = '';
$gcbos = new GCBOS();
$permissions = convert_bits_to_array($vbulletin->userinfo['permissions']['gcbos_permissions'], $vbulletin->bf_ugp_gcbos_permissions);

switch ($_REQUEST['action'])
{
	case 'list_messages':
	case 'send_message':
		break;
	default:
		header("Location: index.php");
		exit;
		break;
}

if (($_POST['action'] == 'send_message') && ($permissions['can_talk']))
{
	$message = trim($_POST['message']);
	
	if (strlen($message) < $vbulletin->options['gcbos_message_min'])
	{
		$result .= $gcbos->build_message(0, sprintf($vbphrase['gcbos_message_too_short'], $vbulletin->options['gcbos_message_min']), 0, TIMENOW, $vbulletin->userinfo['userid']);
	}
	elseif (strlen($message) > $vbulletin->options['gcbos_message_max'])
	{
		$result .= $gcbos->build_message(0, sprintf($vbphrase['gcbos_message_too_long'], $vbulletin->options['gcbos_message_max']), 0, TIMENOW, $vbulletin->userinfo['userid']);
	}
	elseif ((!$gcbos->perform_command($message, &$result)) && (!$gcbos->has_effect(GCBOS_EFFECT_MUTE)))
	{
		$gcbos->create_message($message, $vbulletin->userinfo['userid'], TIMENOW, 0, GCBOS_MESSAGE_TYPE_NORMAL, $vbulletin->userinfo['userid']);
	}
	
	$_REQUEST['action'] = 'list_messages';
}

if (($_REQUEST['action'] == 'list_messages') && ($permissions['can_view']))
{
	$messages = array();
	
	if ($start < 0)
	{
		$result .= '[notice]' . $gcbos->filter_message($gcbos->fetch_notice()) . '[/notice]';
		$messages = $gcbos->fetch_messages(0, $limit, GCBOS_LIMIT_FROM_END, false);
	}
	else
	{
		$messages = $gcbos->fetch_messages($start, $limit, GCBOS_LIMIT_FROM_START, true);
	}
	
	for ($i = 0; $i < count($messages); $i++)
	{
		$message =& $messages[$i];
		
		if ($message['type'] == GCBOS_MESSAGE_TYPE_SYSTEM)
		{
			$split = split(' ', $message['message'], 2);
			$command = $split[0];
			$data = $split[1];
			
			switch ($command)
			{
				case 'notice':
					$result .= '[notice]' . $gcbos->filter_message($data) . '[/notice]';
					break;
				case 'clear':
					$result .= '[clear /]';
					break;
			}
		}
		elseif ($message['type'] == GCBOS_MESSAGE_TYPE_DELETE)
		{
			$result .= '[modify="' . $gcbos->escape_param($message['message'], '"') . '|1|"][/modify]';
		}
		elseif ($message['type'] == GCBOS_MESSAGE_TYPE_EDIT)
		{
			$split = split(' ', $message['message'], 2);
			$gcbosid = $split[0];
			$msg = $split[1];
			$result .= $gcbos->build_message($gcbosid, $msg, $message['displayuserid'], $message['dateline'], $message['recipientid'], $message['userid'], true);
		}
		elseif ($message['type'] == GCBOS_MESSAGE_TYPE_PRIVATE)
		{
			$msg = sprintf($vbphrase['gcbos_private_message'], $message['realname'], $message['message']);
			
			$result .= $gcbos->build_message($message['gcbosid'], $msg, 0, $message['dateline'], $message['recipientid'], $message['userid']);
		}
		elseif ($message['type'] == GCBOS_MESSAGE_TYPE_MUTED)
		{
			$msg = sprintf($vbphrase['gcbos_user_muted'], $message['message'], $message['realname']);
			
			$result .= $gcbos->build_message(0, $msg, 0, $message['dateline'], $message['recipientid'], $message['userid']);
		}
		elseif ($message['type'] == GCBOS_MESSAGE_TYPE_UNMUTED)
		{
			$msg = sprintf($vbphrase['gcbos_user_unmuted'], $message['message'], $message['realname']);
			
			$result .= $gcbos->build_message(0, $msg, 0, $message['dateline'], $message['recipientid'], $message['userid']);
		}
		elseif ($message['type'] == GCBOS_MESSAGE_TYPE_ACTION)
		{
			$result .= $gcbos->build_message($message['gcbosid'], $message['message'], $message['displayuserid'], $message['dateline'], $message['recipientid'], $message['userid'], false, true, false, true);
		}
		else
		{
			$handled = false;
			
			($hook = vBulletinHook::fetch_hook('gcbos_display_message_unknown_type')) ? eval($hook) : false;
			
			if (!$handled)
			{
				$result .= $gcbos->build_message($message['gcbosid'], $message['message'], $message['displayuserid'], $message['dateline'], $message['recipientid'], $message['userid']);
			}
		}
		
		if ($message['gcbosid'] > $last) $last = $message['gcbosid'];
	}
}

header("GCBOS-Start: $last");

if (USING_AJAX)
{
	echo $result;
	if (defined('NOSHUTDOWNFUNC'))
	{
		exec_shut_down();
	}
}
?>